package cn.good.yan.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 演示：
 *     普通接口，演示 sentinel 热点key限流
 *          请求：http://localhost:8401/t3/testD
 *               http://localhost:8401/t3/testD?p1=a        # 会触发
 *               http://localhost:8401/t3/testD?p2=ss&p1=a  # 会触发
 *               http://localhost:8401/t3/testD?p2=a        # 不会触发
 *           参数例外项* - 这里可以配置具体吃参数值，等于某个值会触发限流
 *     SentinelResource
 *
 * @author shengren.yan
 * @create 2024-01-09
 */
@RestController
@RequestMapping("/t3")
@Slf4j
public class Test3Controller {

    @Value("${server.port}")
    private String serverPort;

    // 正常逻辑 - 执行
    @GetMapping("/testD")
    @SentinelResource(value = "t3testD", blockHandler = "dealTestHotKey")
    public String testD(@RequestParam(value = "p1", required = false) String p1,
                        @RequestParam(value = "p2", required = false) String p2) {

        return "------testD -- 正常逻辑返回" ;
    }

    // 触发-降级的方法
    public String dealTestHotKey(String p1, String p2, BlockException exception) {
        return "-----dealTestHotKey 服务降级方法";
    }



}
